package org.anderground.app.view.dialogs;

import org.anderground.core.Logger;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
import android.view.View;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public abstract class BaseDialog {

	/**
	 * It's a pity that we haven't got any possibility to control
	 * {@link AlertDialog}'s onStart() and onStop() events. That is why I am
	 * introducing this subclass.
	 * 
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 */
	private class CustomAlertDialog extends AlertDialog {

		protected CustomAlertDialog(Context context) {
			super(context);
		}

		@Override
		public void onStart() {
			Logger.log("onStart()");
			super.onStart();
		}

		@Override
		protected void onStop() {
			Logger.log("onStop()");
			dialog = null;
			super.onStop();
		}

	}

	/** application context */
	private Context context;

	/** the dialog itself */
	private AlertDialog dialog;

	/** whether the dialog is cancelable or not */
	private Boolean cancelable;
	/** the res id of the image to be used in the title */
	private Drawable icon;
	/** the message to display */
	private CharSequence message;
	/** The resource id of the text to display in the button */
	private CharSequence negativeText, positiveText, neutralText;
	/** The listener to be invoked when the button of the dialog is pressed */
	private OnClickListener negativeListener, positiveListener,
			neutralListener;
	/** the callback that will be called if the dialog is canceled */
	private OnCancelListener onCancelListener;
	/** the title displayed in the Dialog */
	private CharSequence title;
	/** the view to display in the dialog */
	private View view;

	/**
	 * public constructor
	 * 
	 * @param context
	 */
	public BaseDialog(Context context) {
		this.context = context;
	}

	public AlertDialog create() {
		AlertDialog dialog = new CustomAlertDialog(context);
		if (cancelable != null) {
			dialog.setCancelable(cancelable);
		}
		if (icon != null) {
			dialog.setIcon(icon);
		}
		if (message != null) {
			dialog.setMessage(message);
		}
		if (onCancelListener != null) {
			dialog.setOnCancelListener(onCancelListener);
		}
		if (title != null) {
			dialog.setTitle(title);
		}
		if (view != null) {
			dialog.setView(view);
		}
		if (positiveText != null) {
			dialog.setButton(positiveText, positiveListener);
		}
		if (negativeText != null) {
			dialog.setButton2(negativeText, negativeListener);
		}
		if (neutralText != null) {
			dialog.setButton3(neutralText, neutralListener);
		}
		return dialog;
	}

	/**
	 * show the dialog itself
	 */
	protected void show() {
		if (dialog == null) {
			dialog = create();
			dialog.show();
		}
	}

	/**
	 * hide the dialog if it is visible now
	 */
	public void hide() {
		dialog.cancel();
	}

	/**
	 * @param cancelable
	 */
	public void setCancelable(boolean cancelable) {
		this.cancelable = cancelable;
	}

	/**
	 * @param iconId
	 */
	public void setIcon(int iconId) {
		this.icon = context.getResources().getDrawable(iconId);
	}

	/**
	 * @param messageId
	 */
	public void setMessage(int messageId) {
		this.message = context.getText(messageId);
	}

	/**
	 * @param message
	 */
	public void setMessage(CharSequence message) {
		this.message = message;
	}

	/**
	 * @param titleId
	 */
	public void setTitle(int titleId) {
		this.title = context.getText(titleId);
	}

	/**
	 * @param title
	 */
	public void setTitle(CharSequence title) {
		this.title = title;
	}

	/**
	 * @param textId
	 * @param listener
	 */
	public void setNegativeButton(int textId, OnClickListener listener) {
		this.negativeText = context.getText(textId);
		this.negativeListener = listener;
	}

	/**
	 * @param textId
	 * @param listener
	 */
	public void setNeutralButton(int textId, OnClickListener listener) {
		this.neutralText = context.getText(textId);
		this.neutralListener = listener;
	}

	/**
	 * @param textId
	 * @param listener
	 */
	public void setPositiveButton(int textId, OnClickListener listener) {
		this.positiveText = context.getText(textId);
		this.positiveListener = listener;
	}

	/**
	 * @param text
	 * @param listener
	 */
	public void setNegativeButton(CharSequence text, OnClickListener listener) {
		this.negativeText = text;
		this.negativeListener = listener;
	}

	/**
	 * @param text
	 * @param listener
	 */
	public void setNeutralButton(CharSequence text, OnClickListener listener) {
		this.neutralText = text;
		this.neutralListener = listener;
	}

	/**
	 * @param text
	 * @param listener
	 */
	public void setPositiveButton(CharSequence text, OnClickListener listener) {
		this.positiveText = text;
		this.positiveListener = listener;
	}

	/**
	 * @param onCancelListener
	 */
	public void setOnCancelListener(OnCancelListener onCancelListener) {
		this.onCancelListener = onCancelListener;
	}

	/**
	 * @param view
	 */
	public void setView(View view) {
		this.view = view;
	}

	protected AlertDialog getDialog() {
		return dialog;
	}

	protected Context getContext() {
		return context;
	}

}